#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "./lcd/bsp_ili9341_lcd.h"
#include "usart.h"
#include "key.h"
#include "event_groups.h"
#include "stdio.h"

static TaskHandle_t AppTaskCreate_Handle = NULL;
static TimerHandle_t timer1_handle = NULL;
static TimerHandle_t timer2_handle = NULL;

static uint32_t timer_count1 = 0;
static uint32_t timer_count2 = 0;

static void AppTaskCreate(void);

static void Timer1_Callback(void* pvParameters);
static void Timer2_Callback(void* pvParameters);

int main(void)
{ 
	BaseType_t xReturn = pdPASS;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	KEY_Init();
	LED_Init();		        //初始化LED端口
	ILI9341_Init();//液晶初始化    
	LCD_SetColors(BLACK,WHITE);//设置白底黑字
	LCD_SetFont(&Font8x16);//设置字体大小
	ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
  ILI9341_GramScan ( 6 );//设置显示模式
	USART_Config(115200);
  LED_Color(LED_OFF);
	
	xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,  
                        (const char*    )"AppTaskCreate",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )1, 
                        (TaskHandle_t*  )&AppTaskCreate_Handle);
	if(pdPASS == xReturn){
		vTaskStartScheduler();          //开启任务调度
	}
  else{
		printf("Error Start RTOS\n");
	}
	
}


static void AppTaskCreate(void)
{
  BaseType_t xReturn = pdPASS;
  
  taskENTER_CRITICAL();   
    
  timer1_handle = xTimerCreate("AutoReloadTimer", 
                        (TickType_t    )1000,
                        (UBaseType_t       )pdTRUE,   
                        (void*          )1,	    
                        (TimerCallbackFunction_t)Timer1_Callback);
  if(timer1_handle != NULL)
	{
		printf("Success Create TIMER1\r\n");
		xTimerStart(timer1_handle,0);
	}
    
  

   timer2_handle = xTimerCreate("OneShotTimer", 
                        (TickType_t    )5000,
                        (UBaseType_t       )pdFALSE,   
                        (void*          )2,	    
                        (TimerCallbackFunction_t)Timer2_Callback);
  if(timer2_handle != NULL)
	{
		printf("Success Create TIMER2\r\n");
		xTimerStart(timer2_handle,0);
	}
  
  vTaskDelete(AppTaskCreate_Handle);
  
  taskEXIT_CRITICAL();          
}


static void Timer1_Callback(void* pvParameters)
{
	timer_count1++;
	LED_Color(timer_count1%8);
	printf("Timer1 And Number is %d\n",timer_count1);
}
static void Timer2_Callback(void* pvParameters)
{
	printf("Only the time\n");
}
